<?php

namespace App\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SellGoodsDetailModel extends Model
{
    use HasFactory;

    protected $table = 'sell_goods_detail';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'sell_date'
    ];

    static public function getGoodsDetailInfo($sellIdArr, $condition)
    {
        return SELF::select(
            'sell_id'
        )
            ->from('sell_goods_detail as a')
            ->leftJoin('materiel_type as b', 'a.materiel_type_id', '=', 'b.id')
            ->where($condition)
            ->whereIn('sell_id', $sellIdArr)
            ->distinct()
            ->get()->toArray();
    }

    public function goodsBatchInfo()
    {
        return SELF::hasMany(SellGoodsDetailBatchModel::class, 'sell_goods_detail_id', 'id');
    }

    static public function getBatchInfo($sellId)
    {
        return SELF::select(
            'materiel_type_id',
        )
            ->from('sell_goods_detail as a')
            ->leftJoin('sell_goods_detail_batch as b', 'a.id', '=', 'b.sell_goods_detail_id')
            ->where('a.sell_id', $sellId)
            ->get()->toArray();
    }
}
